package com.exampleyyy.demoyyy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exampleyyy.demoyyy.JieKouService.CategoryServiceJieKou;
import com.exampleyyy.demoyyy.ShiTiYYY.Category;
import com.exampleyyy.demoyyy.common.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryServiceJieKou fuwufenleijiekou;

    @PostMapping("")
    public R<Object> save(@RequestBody Category fenlei, HttpSession session){
        System.out.println("前端传递来的数据是"+fenlei);
        System.out.println("session是"+session.getAttribute("员工信息id"));
        Long empid = (Long) session.getAttribute("员工信息id");
//        fenlei.setCreateTime(LocalDateTime.now());
//        fenlei.setUpdateTime(LocalDateTime.now());
//        fenlei.setCreateUser(empid);
//        fenlei.setUpdateUser(empid);
        fenlei.setIsDeleted(1);
        System.out.println("更新后的数据是"+fenlei);
        try {
            fuwufenleijiekou.save(fenlei);
            return R.success(1,"新增分类成功",null);
        }catch (Exception eeee){
            System.out.println("错误是"+eeee);
            return R.error(0,"新增分类失败,分类重复",null);
        }

    }
    @GetMapping("/page")
    public R<Object> page(int page,int pageSize){
        //分页构造器
        Page pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(Category::getSort);
        //分页查询
        fuwufenleijiekou.page(pageInfo,queryWrapper);
        return R.success(1,"分类查询成功",pageInfo);
    }
    @DeleteMapping("")
    public R<Object> delete(Long ids){ // 请求参数
        System.out.println("删除分类，ids为："+ids);
        // fuwufenleijiekou.removeById(ids);
        fuwufenleijiekou.shanchu(ids);
        return R.success(1,"分类信息删除成功",null);
    }
    @PutMapping("")
    public R<Object> update(@RequestBody Category fenlei, HttpSession session){
        System.out.println("修改分类信息："+fenlei);
//        fenlei.setUpdateTime(LocalDateTime.now());
        Long empid = (Long) session.getAttribute("员工信息id");
//        fenlei.setUpdateUser(empid);
        fuwufenleijiekou.updateById(fenlei);
        fuwufenleijiekou.updateById(fenlei);
        return R.success(1,"修改分类信息成功",null);
    }
    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<Object> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = fuwufenleijiekou.list(queryWrapper);
        return R.success(1,"菜品分类查询成功",list);
    }
}
